/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.instrument;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.beans.PropertyChangeEvent;

import twaver.Element;
import twaver.Node;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.chart.DialChart;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;

public class DialAttachment extends ComponentAttachment {

	DialChart dial;
	Element hand;

	public DialAttachment(String name, ElementUI ui) {
		super(name, ui);
		dial = new DialChart();
		dial.setValueTextColor(Color.yellow);
		dial.setMinValue(0);
		dial.setMaxValue(360);
		dial.setStartAngle(0);
		dial.setEndAngle(360);
		dial.setOpaque(false);
		dial.getLegendPane().setVisible(false);
		dial.setScaleColor(new Color(40, 128, 180));
		dial.setScaleLength(10);
		dial.setBallSize(10);
		dial.setValueTextColor(Color.RED);
		dial.setValueTextCenter(true);
		dial.setScaleTextFont(twaver.TUIManager.getDefaultPlainFont().deriveFont(8f));
		dial.setXGap(3);
		dial.setYGap(3);
		dial.setRingGradientFactory(TWaverConst.GRADIENT_LINE_NE);
		
		hand = new Node();
		dial.getDataBox().addElement(hand);
		hand.putChartValue(100);
		hand.putChartColor(new Color(70, 80, 160));

		this.setWidth(180);
		this.setHeight(180);
		this.setBorderStroke(TUIManager.getStrokeByType(TWaverConst.STROKE_ZIGZAG_NARROWEST));
		this.setPosition(TWaverConst.POSITION_CENTER);
		this.setComponent(dial);
	}

    public Shape getShape(){
    	Rectangle rect = dial.getRingBound();
    	Point location = this.getLocation();
		return new Ellipse2D.Double(rect.x+location.x, rect.y+location.y, rect.width, rect.height);
    }

	public void elementPropertyChange(PropertyChangeEvent evt) {
		super.elementPropertyChange(evt);
		if (evt.getPropertyName().equals(TWaverConst.CLIENT_PROPERTY_PREFIX + TWaverConst.PROPERTYNAME_CHART_VALUE)) {
			hand.putChartValue(((Double) evt.getNewValue()).doubleValue());
		}
		else if(evt.getPropertyName().equals(TWaverConst.PROPERTYNAME_SELECTED)){
			if(element.isSelected()){
				dial.setRingGradientFactory(TWaverConst.GRADIENT_LINE_SW);
			}else{
				dial.setRingGradientFactory(TWaverConst.GRADIENT_LINE_NE);
			}
		}
	}
}